package com.example.hangzhou.welbeing.mytestdemoapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;

import com.example.hangzhou.welbeing.mytestdemoapp.R;
import com.example.hangzhou.welbeing.mytestdemoapp.bean.User;
import com.example.hangzhou.welbeing.mytestdemoapp.myInterface.CustomMigration;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;

import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jesse.nativelogger.NLogger;
import io.realm.Realm;
import io.realm.RealmAsyncTask;
import io.realm.RealmConfiguration;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * 杭州惟君医疗科技有限公司10
 * Created by 20433 on 2018/4/8
 *
 * @desc 东风山河绿，春已不折柳，繁花看不尽，处处惹人留
 * @desc 佛祖镇楼，bug辟容易
 */
public class MyOneActivity extends AppCompatActivity {
    @Bind(R.id.button5)
    Button button5;
    @Bind(R.id.textView5)
    TextView textView5;
    @Bind(R.id.textView7)
    TextView textView7;
    private int i = 1;
    private Realm realm;
    private RealmAsyncTask transaction;

    @Bind(R.id.button10)
    Button button10;
    private static int SECOND = 20;
    private Observable<Void> verifyCodeObservable;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_one);
        ButterKnife.bind(this);
        textView5.setText(getIntent().getStringExtra("step"));
        NLogger.e("TAG", "===============1===色图====");


        // Realm mRealm = Realm.getDefaultInstance();
        RealmConfiguration config = new RealmConfiguration
                .Builder()
                .name("myrealm.realm") //文件名
                .schemaVersion(1) //版本号
                .migration(new CustomMigration())//升级数据库
                .build();
        realm = Realm.getInstance(config);
//        RxView.clicks(button10)
//                .throttleFirst(1, TimeUnit.SECONDS)
//                .subscribeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Action1<Void>() {
//                    @Override
//                    public void call(Void aVoid) {
//                        User user2 = realm.where(User.class).findFirst();
//                        textView5.setText(user2.getName());
//                        textView7.setText(user2.getId()+"");
//                        // NLogger.e("TAG", "===============完成查询==user.getAge()==="+user2.getAge() );
//                        NLogger.e("TAG", "===============完成查询==user.getAge()==="+user2.getName() );
//                    }
//                });


        RxView.clicks(button10)
                .throttleFirst(SECOND, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .doOnNext(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        RxView.enabled(button10).call(false);

                    }
                })
                .subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                Observable.interval(1, TimeUnit.SECONDS, AndroidSchedulers.mainThread()).take(SECOND).subscribe(new Observer<Long>() {
                    @Override
                    public void onCompleted() {
                        RxTextView.text(textView5).call("获取验证码");
                        RxView.enabled(button10).call(true);
                    }

                    @Override
                    public void onError(Throwable e) {
                        NLogger.e("TAG", "===============onError===" + e);
                    }

                    @Override
                    public void onNext(Long aLong) {
                        NLogger.e("TAG", "===============aLong===" + aLong);
                        RxTextView.text(textView5).call("剩余" + (SECOND - aLong) + "秒");
                    }
                });
            }
        });


    }

    @OnClick(R.id.button5)
    public void onViewClicked() {
        //成功回调
//失败回调
        transaction = realm.executeTransactionAsync(new Realm.Transaction() {
                                                        @Override
                                                        public void execute(Realm realm) {
                                                            User user = realm.createObject(User.class);
                                                            user.setName("Gavin");
                                                            user.setId("1");
                                                        }
                                                    }, new Realm.Transaction.OnSuccess() {
                                                        @Override
                                                        public void onSuccess() {
                                                            //成功回调
                                                            NLogger.e("TAG", "===============成功回调=====");
                                                        }
                                                    }, new Realm.Transaction.OnError() {
                                                        @Override
                                                        public void onError(Throwable error) { //失败回调
                                                            NLogger.e("TAG", "===============失败回调=====" + error);
                                                        }
                                                    }

        );

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        NLogger.e("TAG", "===============2=额度======" + intent.getStringExtra("step"));
        textView5.setText(intent.getStringExtra("step"));
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (transaction != null && !transaction.isCancelled()) {
            transaction.cancel();
        }
    }
}
